package com.zork.cmdb.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.zork.cmdb.webapi.domain.dbo.AutoFindResultDO;
//import com.zork.cmdb.webapi.domain.dbo.WarehouseInstanceDO;
//import com.zork.cmdb.webapi.domain.dto.AutoFindResultDTO;
//import com.zork.cmdb.webapi.domain.vo.AutoFindResultVO;
//import com.zork.cmdb.webapi.mapper.cmdb.WarehouseInsanceMapper;
//import com.zork.cmdb.webapi.mapper.smartoms.AutoFindResultMapper;
import com.zork.cmdb.webapi.common.exception.CmdbException;
import com.zork.cmdb.webapi.common.exception.CmdbModuleNotFoundException;
import com.zork.cmdb.webapi.domain.dbo.ModuleDO;
import com.zork.cmdb.webapi.domain.mapper.impl.RelationshipInstanceMapperImpl;
import com.zork.cmdb.webapi.service.AutoFindResultSevice;
import com.zork.cmdb.webapi.service.RelationshipInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AutoFindResultServiceImpl implements AutoFindResultSevice {

//    @Autowired
//    private AutoFindResultMapper autoFindResultMapper;
//
//    @Autowired
//    private WarehouseInsanceMapper warehouseInsanceMapper;

    @Autowired
    private RelationshipInstanceService relationshipInstanceService;

    @Autowired
    private RelationshipInstanceMapperImpl relationshipInstanceMapper;

    @Override
    public PageInfo<Map<String, Object>> listAutoFindResult(String sourceInstanceID, Integer pageNum, Integer pageSize) throws CmdbException {
        Map<Integer, ModuleDO> moduleDOMap = relationshipInstanceMapper.getModuleMap();
        if (moduleDOMap == null) {
            relationshipInstanceMapper.loadModuleMap();
            moduleDOMap = relationshipInstanceMapper.getModuleMap();
        }
        ModuleDO hostModuleDO = null;
        for (Map.Entry<Integer, ModuleDO> entry : moduleDOMap.entrySet()) {
            if ("主机".equals(entry.getValue().getModuleName())) {
                hostModuleDO = entry.getValue();
            }
        }
        if (hostModuleDO == null) {
            throw new CmdbModuleNotFoundException("主机模型不存在");
        }
        String whereSql = " AND wi.[CICode] in (SELECT temp.[CICode] FROM (SELECT [CICode], ROW_NUMBER() OVER(PARTITION BY [CICode] ORDER BY [Time] DESC) AS rn FROM [AutoFindConfirm] WHERE [Status] = '已审批' AND [Time] >= DATEADD(d, -3, GETDATE()) AND ([Description] = '自动发现自动确认' OR [Description] IS NULL)) temp WHERE temp.rn = 1)";
        if (sourceInstanceID != null) {
            whereSql += " AND wi.[SourceInstanceID] = '" + sourceInstanceID + "'";
        }
        return relationshipInstanceService.listRelationshipInstance(hostModuleDO.getModuleID(), null,pageNum, pageSize, whereSql);
    }

//    @Override
//    public PageInfo<Object> listAutoFindResult(Integer pageNum, Integer pageSize) {
//        PageInfo<Object> resultPageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> autoFindResultMapper.listAutoFindResult());
//        List<Object> objectList = resultPageInfo.getList();
//        List<AutoFindResultDO> autoFindResultDOList = getAutoFindResultList(objectList);
//        String ciCodeString = getCiCodeString(autoFindResultDOList);
//        List<WarehouseInstanceDO> warehouseInstanceDOList = warehouseInsanceMapper.getWarehouseInstaceByCiCode(ciCodeString);
//        List<AutoFindResultDTO> autoFindResultDTOList = com.zork.cmdb.webapi.domain.mapper.AutoFindResultMapper.MAPPER.getDTOList(autoFindResultDOList, warehouseInstanceDOList);
//        List<AutoFindResultVO> resultVOList = com.zork.cmdb.webapi.domain.mapper.AutoFindResultMapper.MAPPER.getVOList(autoFindResultDTOList);
//        resultPageInfo.setList(Collections.singletonList(resultVOList));
//        return resultPageInfo;
//    }
//
//    @Override
//    public PageInfo<Object> getAutoFindResultBySourceInstanceID(String sourceInstanceID, Integer pageNum, Integer pageSize) {
//        WarehouseInstanceDO warehouseInstanceDO = warehouseInsanceMapper.getWarehouseInsanceBySourceInstanceID(sourceInstanceID);
//        PageInfo<Object> resultPageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> autoFindResultMapper.getAutoFindResultByCiCode(warehouseInstanceDO.getCiCode()));
//        List<Object> objectList = resultPageInfo.getList();
//        List<AutoFindResultDO> autoFindResultDOList = getAutoFindResultList(objectList);
//        List<AutoFindResultDTO> autoFindResultDTOList = com.zork.cmdb.webapi.domain.mapper.AutoFindResultMapper.MAPPER.getDTOList(autoFindResultDOList, warehouseInstanceDO);
//        List<AutoFindResultVO> resultVOList = com.zork.cmdb.webapi.domain.mapper.AutoFindResultMapper.MAPPER.getVOList(autoFindResultDTOList);
//        resultPageInfo.setList(Collections.singletonList(resultVOList));
//        return resultPageInfo;
//    }
//
//    private List<AutoFindResultDO> getAutoFindResultList(List<Object> objectList) {
//        List<AutoFindResultDO> autoFindResultDOList = new ArrayList<>();
//        for(Object object : objectList) {
//            if (object instanceof AutoFindResultDO) {
//                AutoFindResultDO autoFindResultDO = (AutoFindResultDO) object;
//                autoFindResultDOList.add(autoFindResultDO);
//            }
//        }
//        return autoFindResultDOList;
//    }
//
//    private String getCiCodeString(List<AutoFindResultDO> autoFindResultDOList) {
//        Set<String> ciCodeSet = new HashSet<>();
//        for (AutoFindResultDO autoFindResultDO  : autoFindResultDOList) {
//            ciCodeSet.add(autoFindResultDO.getCiCode());
//        }
//        String result = "''";
//        StringBuffer str = new StringBuffer();
//        if (ciCodeSet == null || ciCodeSet.size() == 0) {
//            return result;
//        }
//        else {
//            for (String ciCode : ciCodeSet) {
//                str.append("'" + ciCode + "',");
//            }
//            result = str.substring(0, str.length() - 1);
//        }
//        return result;
//    }
}
